package dao;

import util.JdbcHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UniversityDao {
    public boolean add(University university, Connection connection) throws SQLException {
        PreparedStatement preparedStatement = connection.prepareStatement("insert into university (id,description,no,remarks) values (?,?,?,?)");
        preparedStatement.setLong(1,university.getId());
        preparedStatement.setString(2,university.getDescription());
        preparedStatement.setString(3,university.getNo());
        preparedStatement.setString(4,university.getRemarks());
        int row = preparedStatement.executeUpdate();
        JdbcHelper.close(null,preparedStatement);
        return row > 0;
    }
    public boolean update(University university) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        //准备预编译的语句
        String sql_update = "UPDATE university SET description = ?, no= ?,remarks=? WHERE id = ?";
        PreparedStatement pstmt_update =
                connection.prepareStatement(sql_update);

        pstmt_update.setString(1, university.getDescription());
        pstmt_update.setString(2, university.getNo());
        pstmt_update.setString(3,university.getRemarks());

    public static UniversityDao universityDao = new UniversityDao();
    public static UniversityDao getInstance(){
        return universityDao;
    }

    public boolean delete(int id) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        //准备预编译的语句
        String sql_delete_id = "DELETE FROM university WHERE id = ?";
        PreparedStatement pstmt_delete =
                connection.prepareStatement(sql_delete_id);
        pstmt_delete.setInt(1, id);
        //执行预编译语句，影响的行数保存在rowAffected中
        int rowAffected = pstmt_delete.executeUpdate();
        JdbcHelper.close(pstmt_delete, connection);
        return rowAffected > 0;
        System.out.println();
    }
        //执行预编译语句，影响的行数保存在rowAffected中
        int rowAffected = pstmt_update.executeUpdate();

        JdbcHelper.close(pstmt_update, connection);
        return rowAffected > 0;
    }
}
